﻿using UnityEngine;
using System.Collections;

public class AnimationReplace : MonoBehaviour {
	[SerializeField] float height = 5f;
	[SerializeField] float riseTime = 1f;
	[SerializeField] float rotateTime = 2f;
	[SerializeField] float totalAngle = 1800f;

	int status = 0;
	float time = 0f;
	float lastResult = 0f;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		time += Time.deltaTime;
		float curResult;
		switch (status) {
		case 0:
			if (time > riseTime) {
				transform.position += new Vector3 (0f, height - lastResult, 0f);
				lastResult = 0f;
				time = 0f;
				status++;
				return;
			}
			//2.5cos(t/riseTime*Mathf.PI) + 2.5
			//transform.position += new Vector3(0f, Mathf.PI / riseTime * Mathf.Sin(time/riseTime*Mathf.PI) * height / 2f * Time.deltaTime, 0f);
			curResult = (-Mathf.Cos (time / riseTime * Mathf.PI) + 1f) * height / 2f;
			transform.position += new Vector3 (0f, curResult - lastResult, 0f);
			lastResult = curResult;
			break;
		case 1:
			if (time > rotateTime) {
				transform.RotateAround (transform.position, Vector3.up, totalAngle-lastResult);
				lastResult = 0f;
				time = 0f;
				status++;
				return;
			}
			//transform.RotateAround (transform.position, Vector3.up, 
			//	Mathf.PI / rotateTime * Mathf.Sin (time / rotateTime * Mathf.PI) * totalAngle / 2f * Time.deltaTime);
			curResult = (-Mathf.Cos (time / rotateTime * Mathf.PI) + 1f) * totalAngle / 2f;
			transform.RotateAround (transform.position, Vector3.up, curResult-lastResult);
			lastResult = curResult;
			break;
		case 2:
			if (time > riseTime) {
				transform.position -= new Vector3 (0f, height - lastResult, 0f);
				lastResult = 0f;
				time = 0f;
				status++;
				return;
			}
			//transform.position -= new Vector3(0f, Mathf.PI / riseTime * Mathf.Sin(time/riseTime*Mathf.PI) * height / 2f * Time.deltaTime, 0f);
			curResult = (-Mathf.Cos (time / riseTime * Mathf.PI) + 1f) * height / 2f;
			transform.position -= new Vector3 (0f, curResult - lastResult, 0f);
			lastResult = curResult;
			break;
		default:
			break;
		}
	}
}
